//
//  CourseDetailViewController.m
//  UADE-Mobile
//
//  Created by Marcos Damian Pianelli on 9/21/11.
//  Copyright (c) 2011 DG. All rights reserved.
//

#import "CourseDetailViewController.h"
#import "TTTableMarkItem.h"
#import "TTCustomDataSource.h"






@implementation CourseDetailViewController
@synthesize  userInfo;


- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
   
    self = [super initWithNibName:@"CourseDetailViewController" bundle:nil];

    if (self) {
        
        
        self.title = [query objectForKey:@"NombreMateria"];
        self.variableHeightRows = YES;
        self.userInfo = query;
        NSLog(@"%@", [query description]);
      /*  
        TTURLMap* map = [TTNavigator navigator].URLMap;
        [map from:@"tt://downloadAction" toObject:self selector:@selector(openDownloadSection)];
        [map from:@"tt://newsAction" toObject:self selector:@selector(openNewsSection)];
*/

        
        // Uncomment this to see how the table looks with the grouped style
        self.tableViewStyle = UITableViewStyleGrouped;
        
        // Uncomment this to see how the table cells look against a custom background color
        //self.tableView.backgroundColor = [UIColor yellowColor];
 
        NSString * nroAula = [query objectForKey:@"NumeroAula"];
        NSString * fechaExamen = [[query objectForKey:@"FechaExamen"] substringToIndex:10];
        
        NSString * aula = [NSString stringWithFormat:@"Aula: %@",nroAula];
        NSString * horario = [NSString stringWithFormat:@"%@ %@",[query objectForKey:@"DiasCursada"],[query objectForKey:@"Horario"]];        
        NSString * examenFinal = [NSString stringWithFormat:@"Exámen : %@",fechaExamen];
        NSString * asistencia = [NSString stringWithFormat:@"Asistencia : %@",[query objectForKey:@"Asistencia"]];

        if(![[query objectForKey:@"Asistencia"] isEqualToString:@"-"]){
            asistencia = [asistencia stringByAppendingString:@"%"];
        }
        
        
        NSMutableArray * generalInfoTableItems = [NSMutableArray arrayWithObjects:
                                           [TTTableImageItem itemWithText:horario imageURL:@"bundle://icono_fecha.jpg"
                                                                      URL:nil],
                                            [TTTableImageItem itemWithText:asistencia imageURL:@"bundle://icn_porcentaje.png"
                                                                             URL:nil],
                                                  
                                           [TTTableImageItem itemWithText:examenFinal imageURL:@"bundle://icono_fecha_final.jpg"
                                                                      URL:nil],     
                                            nil];
        
        
        if( ![nroAula isKindOfClass:[NSNull class]]){
            [generalInfoTableItems insertObject:[TTTableImageItem itemWithText:aula imageURL:@"bundle://icono_aula.jpg"
                                                                           URL:nil] atIndex:0];
        }
        
       
    
        NSString * downloadURL = [NSString stringWithFormat:@"tt://download/%@",[self.userInfo objectForKey:@"IDAula"]];
        NSString * newsURL =  [NSString stringWithFormat:@"tt://news/%@",[self.userInfo objectForKey:@"IDAula"]];
        NSString * foroURL =  [NSString stringWithFormat:@"tt://foro/%@",[self.userInfo objectForKey:@"IDAula"]];


        
        NSArray * optionsTableItems = [NSArray arrayWithObjects:
                                       
                        [TTTableImageItem itemWithText:NSLocalizedString(@"Groups", @"") imageURL:@"bundle://btn_foro.jpg"
                                                                  URL:foroURL],
                                       
                        [TTTableImageItem itemWithText:NSLocalizedString(@"News", @"") imageURL:@"bundle://btn_noticias.jpg"
                                            URL:newsURL],
                                       
                        [TTTableImageItem itemWithText:NSLocalizedString(@"Download", @"") imageURL:@"bundle://btn_descargar.jpg" 
                                            URL:downloadURL],  nil];
        
        
        
        NSMutableArray * markTableItems = [NSMutableArray array];
        NSArray * marks = [query objectForKey:@"Notas"];
        

        for( NSDictionary * eachMark in marks){
            
            [markTableItems addObject: [TTTableMarkItem itemWithText:[eachMark objectForKey:@"Nombre"] mark:[eachMark objectForKey:@"Valor"] ]];
            
        }
        
        
        
        // This demonstrates how to create a table with standard table "fields".  Many of these
        // fields with URLs that will be visited when the row is selected
        self.dataSource = [TTCustomDataSource dataSourceWithArrays:
                           NSLocalizedString(@"General Information", @""),
                           generalInfoTableItems,
                           NSLocalizedString(@"Exams", @""),
                           markTableItems,
                           NSLocalizedString(@"Options", @""),
                           optionsTableItems,
                           nil];
                                                     
                           

        
               
        
    }
    
    return self;
}

/*
-(void)openDownloadSection{
    
    [[TTNavigator navigator] openURLAction:
     [[[TTURLAction actionWithURLPath:@"tt://download"] applyQuery:[self.userInfo objectForKey:@"IDAula"]] applyAnimated:TRUE]];
    

}

-(void)openNewsSection{
    
    [[TTNavigator navigator] openURLAction:
     [[[TTURLAction actionWithURLPath:@"tt://news"] applyQuery:[self.userInfo objectForKey:@"IDAula"]] applyAnimated:TRUE]];
    
    
}
*/

// CREAR URL a un metodo
/*- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath{
    

    
}*/



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
   /* TTURLMap* map = [TTNavigator navigator].URLMap;
    [map removeURL:@"tt://downloadAction"];
    [map removeURL:@"tt://newsAction"];
*/
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
